const app=getApp()
Page({
  data: {
    total:0,
    totalPrice:0.0,
    address:''
  },

  onLoad: function(options) {
    console.log(app.globalData.orderList)
    this.setData({
      orderList:app.globalData.orderList
    })
    console.log(app.globalData.orderList)
  },
  // 点击增加按钮
  plusClick(event){
    console.log(event)
    var index =event.target.dataset.index
    var orderList =this.data.orderList
    console.log(orderList[index].number)
    ++orderList[index].number
    this.setData({
      "orderList":orderList
    })
    this.totalize();
  },
  // 点击减少按钮
  reduceClick(event){
    var index =event.target.dataset.index
    var orderList =this.data.orderList
    var goodNum = orderList[index].number
    if(goodNum<=0){
      goodNum=0;
    }else{
      --orderList[index].number
    }
    this.setData({
      "orderList":orderList
    })
    this.totalize();
  },
  totalize(){
    var orderList =this.data.orderList
    var total = 0
    var totalPrice = 0.0

    for(var i=0;i<orderList.length;i++){
      var good = orderList[i];
      // if(good.choose){
        total = total + good.number;
        totalPrice = totalPrice + good.number*good.price
      // }
    }
    totalPrice = totalPrice.toFixed(2)
    this.setData({
      "total":total,
      "totalPrice": totalPrice
    })
  },
  addAddress() {  
    let that =this;
    wx.chooseAddress({  
      success: (result)=>{
        console.log(result)
        that.setData({
          phone: result.telNumber,
          name: result.userName,
          address: result.provinceName+result.cityName+result.detailInfo
        })
      },
    })
  } ,
  //弹窗
  async handlePopup(e){
    const orderList = this.data.orderList;
    const db = wx.cloud.database();
    const res = await wx.cloud.callFunction({
      name: 'getOpenai'
    });
    const openid = res.result.openid;
    const address=this.data.address
    console.log(address);
    if(address==''){
      wx.showToast({
        title: '请完善地址信息',
        icon: 'none'
      });
      return
    }
    if(this.data.orderList[0].number==0){
      wx.showToast({
        title: '请至少选择一个商品',
        icon: 'none'
      });
      return
    }
      db.collection('shop_order').add({
        data: {
          name: this.data.name,
          phone: this.data.phone,
          address: this.data.address,
          openid: openid, // 根据实际情况设置 openid
          goods:orderList,
          state:1,
          total:this.data.total,
          totalPrice:this.data.totalPrice
        }
      })
      wx.showToast({
        title: '商品添加成功',
        icon: 'none'
      });
    

    //弹出中间框
    const { item } = e.currentTarget.dataset;
        this.setData(
          {
            cur: item,
          },
          () => {
            this.setData({ visible: true });
          },
        );
     },
     onVisibleChange(e) {
      this.setData({
        visible: e.detail.visible,
      });
    },
    //更新状态支付数据库，更新 shop_order 数据库中每个商品状态为 1
    async onClose() {
      // 更新 shop_order 数据库中每个商品状态为 1
      const orderList = this.data.orderList;
      const db = wx.cloud.database();
      const res = await wx.cloud.callFunction({
        name: 'getOpenai',
      });
      const openid = res.result.openid;
      const query = db
       .collection('shop_order')
       .where({ openid: openid });
      // const updateTasks = [];
      //更新状态支付数据库
      console.log(query);
      // for (const order of orderList) {
      //   updateTasks.push(
      //     query.where({ good_id:order.good_id }).update({
      //       data: {
      //         state: 1,
      //       },
      //     })
      //   );
      // }
      // try {
      //   await Promise.all(updateTasks);
      //   wx.showToast({
      //     title: '商品状态更新成功',
      //     icon: 'success',
      //   });
      // } catch (error) {
      //   wx.showToast({
      //     title: '商品状态更新失败',
      //     icon: 'none',
      //   });
      //   console.error(error);
      // }
       //更新弹出框的关闭状态
       this.setData({
        visible: false,
      });
      wx.navigateBack({
        delta: 2
      })
    }
})